#include "kdcsubtitledelegate.h"

#include <KComboBox>
#include <KIntNumInput>
#include <KDoubleNumInput>

#include <QPainter>
#include <QApplication>

#include "kdcsubtitlemodel.h"
#include <kdctextcodeccombobox.h>

KDCSubtitleDelegate::KDCSubtitleDelegate( QObject *aParent ) : QItemDelegate( aParent )
{

}

QWidget* KDCSubtitleDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	switch ( index.column() )
	{
		case 1: return new KDCTextCodecComboBox( parent );
		case 2: return new KDoubleNumInput( parent );
		case 3: return new KIntNumInput( parent );
	}
	return NULL;
}

void KDCSubtitleDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex& index ) const
{
	switch ( index.column() )
	{
		case 1: model->setData( index, static_cast<KDCTextCodecComboBox*>( editor )->currentCodec(), Qt::EditRole ); break;
		case 2: model->setData( index, static_cast<KDoubleNumInput*>( editor )->value(), Qt::EditRole ); break;
		case 3: model->setData( index, static_cast<KIntNumInput*>( editor )->value(), Qt::EditRole ); break;
	}
}

void KDCSubtitleDelegate::setEditorData( QWidget * editor, const QModelIndex & index ) const
{
	switch ( index.column() )
	{
		case 1: static_cast<KDCTextCodecComboBox*>( editor )->setCurrentCodec( index.data( Qt::EditRole ).toString() ); break;
		case 2: static_cast<KDoubleNumInput*>( editor )->setValue( index.data( Qt::EditRole ).toDouble() ); break;
		case 3: static_cast<KIntNumInput*>( editor )->setValue( index.data( Qt::EditRole ).toInt() ); break;
	}
}

void KDCSubtitleDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex& index ) const
{
	int tRow = index.data( KDCSubtitleModel::MovieTitleRowRole ).toInt();
	
	if ( index.row() == tRow && index.column() == 0 )
	{
	  painter->save();
	  QStyleOptionButton opt;
	  opt.features |= QStyleOptionButton::None;
	  opt.rect = option.rect;
	  opt.state |= QStyle::State_Off;
	  opt.text = index.data( Qt::DisplayRole ).toString();
	  QApplication::style()->drawControl(QStyle::CE_PushButton, &opt, painter, 0);
	  painter->restore();
	}
	if ( index.row() == tRow )
		return;
	drawDisplay( painter, option, option.rect, index.data( Qt::DisplayRole ).toString() );
}
